{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Compare features to reference variable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from feature_engine.creation import RelativeFeatures\n",
    "from sklearn.datasets import load_breast_cancer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>mean radius</th>\n",
       "      <th>mean texture</th>\n",
       "      <th>mean perimeter</th>\n",
       "      <th>mean area</th>\n",
       "      <th>mean smoothness</th>\n",
       "      <th>mean compactness</th>\n",
       "      <th>mean concavity</th>\n",
       "      <th>mean concave points</th>\n",
       "      <th>mean symmetry</th>\n",
       "      <th>mean fractal dimension</th>\n",
       "      <th>...</th>\n",
       "      <th>worst radius</th>\n",
       "      <th>worst texture</th>\n",
       "      <th>worst perimeter</th>\n",
       "      <th>worst area</th>\n",
       "      <th>worst smoothness</th>\n",
       "      <th>worst compactness</th>\n",
       "      <th>worst concavity</th>\n",
       "      <th>worst concave points</th>\n",
       "      <th>worst symmetry</th>\n",
       "      <th>worst fractal dimension</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>17.99</td>\n",
       "      <td>10.38</td>\n",
       "      <td>122.80</td>\n",
       "      <td>1001.0</td>\n",
       "      <td>0.11840</td>\n",
       "      <td>0.27760</td>\n",
       "      <td>0.3001</td>\n",
       "      <td>0.14710</td>\n",
       "      <td>0.2419</td>\n",
       "      <td>0.07871</td>\n",
       "      <td>...</td>\n",
       "      <td>25.38</td>\n",
       "      <td>17.33</td>\n",
       "      <td>184.60</td>\n",
       "      <td>2019.0</td>\n",
       "      <td>0.1622</td>\n",
       "      <td>0.6656</td>\n",
       "      <td>0.7119</td>\n",
       "      <td>0.2654</td>\n",
       "      <td>0.4601</td>\n",
       "      <td>0.11890</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>20.57</td>\n",
       "      <td>17.77</td>\n",
       "      <td>132.90</td>\n",
       "      <td>1326.0</td>\n",
       "      <td>0.08474</td>\n",
       "      <td>0.07864</td>\n",
       "      <td>0.0869</td>\n",
       "      <td>0.07017</td>\n",
       "      <td>0.1812</td>\n",
       "      <td>0.05667</td>\n",
       "      <td>...</td>\n",
       "      <td>24.99</td>\n",
       "      <td>23.41</td>\n",
       "      <td>158.80</td>\n",
       "      <td>1956.0</td>\n",
       "      <td>0.1238</td>\n",
       "      <td>0.1866</td>\n",
       "      <td>0.2416</td>\n",
       "      <td>0.1860</td>\n",
       "      <td>0.2750</td>\n",
       "      <td>0.08902</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>19.69</td>\n",
       "      <td>21.25</td>\n",
       "      <td>130.00</td>\n",
       "      <td>1203.0</td>\n",
       "      <td>0.10960</td>\n",
       "      <td>0.15990</td>\n",
       "      <td>0.1974</td>\n",
       "      <td>0.12790</td>\n",
       "      <td>0.2069</td>\n",
       "      <td>0.05999</td>\n",
       "      <td>...</td>\n",
       "      <td>23.57</td>\n",
       "      <td>25.53</td>\n",
       "      <td>152.50</td>\n",
       "      <td>1709.0</td>\n",
       "      <td>0.1444</td>\n",
       "      <td>0.4245</td>\n",
       "      <td>0.4504</td>\n",
       "      <td>0.2430</td>\n",
       "      <td>0.3613</td>\n",
       "      <td>0.08758</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>11.42</td>\n",
       "      <td>20.38</td>\n",
       "      <td>77.58</td>\n",
       "      <td>386.1</td>\n",
       "      <td>0.14250</td>\n",
       "      <td>0.28390</td>\n",
       "      <td>0.2414</td>\n",
       "      <td>0.10520</td>\n",
       "      <td>0.2597</td>\n",
       "      <td>0.09744</td>\n",
       "      <td>...</td>\n",
       "      <td>14.91</td>\n",
       "      <td>26.50</td>\n",
       "      <td>98.87</td>\n",
       "      <td>567.7</td>\n",
       "      <td>0.2098</td>\n",
       "      <td>0.8663</td>\n",
       "      <td>0.6869</td>\n",
       "      <td>0.2575</td>\n",
       "      <td>0.6638</td>\n",
       "      <td>0.17300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>20.29</td>\n",
       "      <td>14.34</td>\n",
       "      <td>135.10</td>\n",
       "      <td>1297.0</td>\n",
       "      <td>0.10030</td>\n",
       "      <td>0.13280</td>\n",
       "      <td>0.1980</td>\n",
       "      <td>0.10430</td>\n",
       "      <td>0.1809</td>\n",
       "      <td>0.05883</td>\n",
       "      <td>...</td>\n",
       "      <td>22.54</td>\n",
       "      <td>16.67</td>\n",
       "      <td>152.20</td>\n",
       "      <td>1575.0</td>\n",
       "      <td>0.1374</td>\n",
       "      <td>0.2050</td>\n",
       "      <td>0.4000</td>\n",
       "      <td>0.1625</td>\n",
       "      <td>0.2364</td>\n",
       "      <td>0.07678</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 30 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   mean radius  mean texture  mean perimeter  mean area  mean smoothness  \\\n",
       "0        17.99         10.38          122.80     1001.0          0.11840   \n",
       "1        20.57         17.77          132.90     1326.0          0.08474   \n",
       "2        19.69         21.25          130.00     1203.0          0.10960   \n",
       "3        11.42         20.38           77.58      386.1          0.14250   \n",
       "4        20.29         14.34          135.10     1297.0          0.10030   \n",
       "\n",
       "   mean compactness  mean concavity  mean concave points  mean symmetry  \\\n",
       "0           0.27760          0.3001              0.14710         0.2419   \n",
       "1           0.07864          0.0869              0.07017         0.1812   \n",
       "2           0.15990          0.1974              0.12790         0.2069   \n",
       "3           0.28390          0.2414              0.10520         0.2597   \n",
       "4           0.13280          0.1980              0.10430         0.1809   \n",
       "\n",
       "   mean fractal dimension  ...  worst radius  worst texture  worst perimeter  \\\n",
       "0                 0.07871  ...         25.38          17.33           184.60   \n",
       "1                 0.05667  ...         24.99          23.41           158.80   \n",
       "2                 0.05999  ...         23.57          25.53           152.50   \n",
       "3                 0.09744  ...         14.91          26.50            98.87   \n",
       "4                 0.05883  ...         22.54          16.67           152.20   \n",
       "\n",
       "   worst area  worst smoothness  worst compactness  worst concavity  \\\n",
       "0      2019.0            0.1622             0.6656           0.7119   \n",
       "1      1956.0            0.1238             0.1866           0.2416   \n",
       "2      1709.0            0.1444             0.4245           0.4504   \n",
       "3       567.7            0.2098             0.8663           0.6869   \n",
       "4      1575.0            0.1374             0.2050           0.4000   \n",
       "\n",
       "   worst concave points  worst symmetry  worst fractal dimension  \n",
       "0                0.2654          0.4601                  0.11890  \n",
       "1                0.1860          0.2750                  0.08902  \n",
       "2                0.2430          0.3613                  0.08758  \n",
       "3                0.2575          0.6638                  0.17300  \n",
       "4                0.1625          0.2364                  0.07678  \n",
       "\n",
       "[5 rows x 30 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# load the breast cancer dataset from sklearn\n",
    "data = load_breast_cancer()\n",
    "\n",
    "# create a dataframe with the independent variables\n",
    "df = pd.DataFrame(data.data, columns=data.feature_names)\n",
    "\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "# # print description of dataset\n",
    "\n",
    "# print(data.DESCR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0.38800\n",
       "1    0.10796\n",
       "2    0.26460\n",
       "3    0.58240\n",
       "4    0.07220\n",
       "Name: difference, dtype: float64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Difference between 2 features - method 1\n",
    "\n",
    "df[\"difference\"] = df[\"worst compactness\"].sub(df[\"mean compactness\"])\n",
    "\n",
    "df[\"difference\"].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0.38800\n",
       "1    0.10796\n",
       "2    0.26460\n",
       "3    0.58240\n",
       "4    0.07220\n",
       "Name: difference, dtype: float64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Difference between 2 features - method 2\n",
    "\n",
    "df[\"difference\"] = df[\"worst compactness\"] - (df[\"mean compactness\"])\n",
    "\n",
    "df[\"difference\"].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    1.410784\n",
       "1    1.214876\n",
       "2    1.197054\n",
       "3    1.305604\n",
       "4    1.110892\n",
       "Name: quotient, dtype: float64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Quotient of features - method 1\n",
    "\n",
    "df[\"quotient\"] = df[\"worst radius\"].div(df[\"mean radius\"])\n",
    "\n",
    "df[\"quotient\"].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    1.410784\n",
       "1    1.214876\n",
       "2    1.197054\n",
       "3    1.305604\n",
       "4    1.110892\n",
       "Name: quotient, dtype: float64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Quotient of features - method 2\n",
    "\n",
    "df[\"quotient\"] = df[\"worst radius\"] / (df[\"mean radius\"])\n",
    "df[\"quotient\"].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# features of interest\n",
    "features = [\"mean smoothness\", \"mean compactness\", \"mean concavity\", \"mean symmetry\"]\n",
    "\n",
    "# reference features\n",
    "reference = [\"mean radius\", \"mean area\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# combine multiple variables with multiple references\n",
    "\n",
    "creator = RelativeFeatures(\n",
    "    variables=features,\n",
    "    reference=reference,\n",
    "    func=[\"sub\", \"div\"],\n",
    ")\n",
    "\n",
    "df_t = creator.fit_transform(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['mean smoothness_sub_mean radius',\n",
       " 'mean compactness_sub_mean radius',\n",
       " 'mean concavity_sub_mean radius',\n",
       " 'mean symmetry_sub_mean radius',\n",
       " 'mean smoothness_sub_mean area',\n",
       " 'mean compactness_sub_mean area',\n",
       " 'mean concavity_sub_mean area',\n",
       " 'mean symmetry_sub_mean area',\n",
       " 'mean smoothness_div_mean radius',\n",
       " 'mean compactness_div_mean radius',\n",
       " 'mean concavity_div_mean radius',\n",
       " 'mean symmetry_div_mean radius',\n",
       " 'mean smoothness_div_mean area',\n",
       " 'mean compactness_div_mean area',\n",
       " 'mean concavity_div_mean area',\n",
       " 'mean symmetry_div_mean area']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Name of new features\n",
    "\n",
    "new_features = [f for f in df_t.columns if f not in creator.feature_names_in_]\n",
    "\n",
    "new_features"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>mean smoothness_sub_mean radius</th>\n",
       "      <th>mean compactness_sub_mean radius</th>\n",
       "      <th>mean concavity_sub_mean radius</th>\n",
       "      <th>mean symmetry_sub_mean radius</th>\n",
       "      <th>mean smoothness_sub_mean area</th>\n",
       "      <th>mean compactness_sub_mean area</th>\n",
       "      <th>mean concavity_sub_mean area</th>\n",
       "      <th>mean symmetry_sub_mean area</th>\n",
       "      <th>mean smoothness_div_mean radius</th>\n",
       "      <th>mean compactness_div_mean radius</th>\n",
       "      <th>mean concavity_div_mean radius</th>\n",
       "      <th>mean symmetry_div_mean radius</th>\n",
       "      <th>mean smoothness_div_mean area</th>\n",
       "      <th>mean compactness_div_mean area</th>\n",
       "      <th>mean concavity_div_mean area</th>\n",
       "      <th>mean symmetry_div_mean area</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-17.87160</td>\n",
       "      <td>-17.71240</td>\n",
       "      <td>-17.6899</td>\n",
       "      <td>-17.7481</td>\n",
       "      <td>-1000.88160</td>\n",
       "      <td>-1000.72240</td>\n",
       "      <td>-1000.6999</td>\n",
       "      <td>-1000.7581</td>\n",
       "      <td>0.006581</td>\n",
       "      <td>0.015431</td>\n",
       "      <td>0.016681</td>\n",
       "      <td>0.013446</td>\n",
       "      <td>0.000118</td>\n",
       "      <td>0.000277</td>\n",
       "      <td>0.000300</td>\n",
       "      <td>0.000242</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-20.48526</td>\n",
       "      <td>-20.49136</td>\n",
       "      <td>-20.4831</td>\n",
       "      <td>-20.3888</td>\n",
       "      <td>-1325.91526</td>\n",
       "      <td>-1325.92136</td>\n",
       "      <td>-1325.9131</td>\n",
       "      <td>-1325.8188</td>\n",
       "      <td>0.004120</td>\n",
       "      <td>0.003823</td>\n",
       "      <td>0.004225</td>\n",
       "      <td>0.008809</td>\n",
       "      <td>0.000064</td>\n",
       "      <td>0.000059</td>\n",
       "      <td>0.000066</td>\n",
       "      <td>0.000137</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-19.58040</td>\n",
       "      <td>-19.53010</td>\n",
       "      <td>-19.4926</td>\n",
       "      <td>-19.4831</td>\n",
       "      <td>-1202.89040</td>\n",
       "      <td>-1202.84010</td>\n",
       "      <td>-1202.8026</td>\n",
       "      <td>-1202.7931</td>\n",
       "      <td>0.005566</td>\n",
       "      <td>0.008121</td>\n",
       "      <td>0.010025</td>\n",
       "      <td>0.010508</td>\n",
       "      <td>0.000091</td>\n",
       "      <td>0.000133</td>\n",
       "      <td>0.000164</td>\n",
       "      <td>0.000172</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-11.27750</td>\n",
       "      <td>-11.13610</td>\n",
       "      <td>-11.1786</td>\n",
       "      <td>-11.1603</td>\n",
       "      <td>-385.95750</td>\n",
       "      <td>-385.81610</td>\n",
       "      <td>-385.8586</td>\n",
       "      <td>-385.8403</td>\n",
       "      <td>0.012478</td>\n",
       "      <td>0.024860</td>\n",
       "      <td>0.021138</td>\n",
       "      <td>0.022741</td>\n",
       "      <td>0.000369</td>\n",
       "      <td>0.000735</td>\n",
       "      <td>0.000625</td>\n",
       "      <td>0.000673</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-20.18970</td>\n",
       "      <td>-20.15720</td>\n",
       "      <td>-20.0920</td>\n",
       "      <td>-20.1091</td>\n",
       "      <td>-1296.89970</td>\n",
       "      <td>-1296.86720</td>\n",
       "      <td>-1296.8020</td>\n",
       "      <td>-1296.8191</td>\n",
       "      <td>0.004943</td>\n",
       "      <td>0.006545</td>\n",
       "      <td>0.009759</td>\n",
       "      <td>0.008916</td>\n",
       "      <td>0.000077</td>\n",
       "      <td>0.000102</td>\n",
       "      <td>0.000153</td>\n",
       "      <td>0.000139</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   mean smoothness_sub_mean radius  mean compactness_sub_mean radius  \\\n",
       "0                        -17.87160                         -17.71240   \n",
       "1                        -20.48526                         -20.49136   \n",
       "2                        -19.58040                         -19.53010   \n",
       "3                        -11.27750                         -11.13610   \n",
       "4                        -20.18970                         -20.15720   \n",
       "\n",
       "   mean concavity_sub_mean radius  mean symmetry_sub_mean radius  \\\n",
       "0                        -17.6899                       -17.7481   \n",
       "1                        -20.4831                       -20.3888   \n",
       "2                        -19.4926                       -19.4831   \n",
       "3                        -11.1786                       -11.1603   \n",
       "4                        -20.0920                       -20.1091   \n",
       "\n",
       "   mean smoothness_sub_mean area  mean compactness_sub_mean area  \\\n",
       "0                    -1000.88160                     -1000.72240   \n",
       "1                    -1325.91526                     -1325.92136   \n",
       "2                    -1202.89040                     -1202.84010   \n",
       "3                     -385.95750                      -385.81610   \n",
       "4                    -1296.89970                     -1296.86720   \n",
       "\n",
       "   mean concavity_sub_mean area  mean symmetry_sub_mean area  \\\n",
       "0                    -1000.6999                   -1000.7581   \n",
       "1                    -1325.9131                   -1325.8188   \n",
       "2                    -1202.8026                   -1202.7931   \n",
       "3                     -385.8586                    -385.8403   \n",
       "4                    -1296.8020                   -1296.8191   \n",
       "\n",
       "   mean smoothness_div_mean radius  mean compactness_div_mean radius  \\\n",
       "0                         0.006581                          0.015431   \n",
       "1                         0.004120                          0.003823   \n",
       "2                         0.005566                          0.008121   \n",
       "3                         0.012478                          0.024860   \n",
       "4                         0.004943                          0.006545   \n",
       "\n",
       "   mean concavity_div_mean radius  mean symmetry_div_mean radius  \\\n",
       "0                        0.016681                       0.013446   \n",
       "1                        0.004225                       0.008809   \n",
       "2                        0.010025                       0.010508   \n",
       "3                        0.021138                       0.022741   \n",
       "4                        0.009759                       0.008916   \n",
       "\n",
       "   mean smoothness_div_mean area  mean compactness_div_mean area  \\\n",
       "0                       0.000118                        0.000277   \n",
       "1                       0.000064                        0.000059   \n",
       "2                       0.000091                        0.000133   \n",
       "3                       0.000369                        0.000735   \n",
       "4                       0.000077                        0.000102   \n",
       "\n",
       "   mean concavity_div_mean area  mean symmetry_div_mean area  \n",
       "0                      0.000300                     0.000242  \n",
       "1                      0.000066                     0.000137  \n",
       "2                      0.000164                     0.000172  \n",
       "3                      0.000625                     0.000673  \n",
       "4                      0.000153                     0.000139  "
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# new features\n",
    "\n",
    "df_t[new_features].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# https://pandas.pydata.org/pandas-docs/stable/reference/frame.html#binary-operator-functions"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "fsml",
   "language": "python",
   "name": "fsml"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": "block",
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
